<script setup lang="ts">
import type { ConsultType } from '@/enums';
import ConsultItem from './ConsultItem.vue'
import { ref } from 'vue';
import type { ConsultOrderItem, ConsultOrderParams } from '@/types/consult';
import { getConsultOrderList } from '@/services/consult';
// 1.准备好查询参数
// 2.实现加载更多效果
// 3.触发加载更多操作时，发请求获取数据
// 4.拿到数据后： 结束加载状态
// 4.1 有数据 添加列表
// 4.2 无数据 追加列表
const props = defineProps<{
  types: ConsultType
}>()
const params = ref<ConsultOrderParams>({
  type: props.types,
  current: 1,
  pageSize: 5
})
const loading = ref(false)
const finished = ref(false)
const list = ref<ConsultOrderItem[]>([])
// 不满一屏触发
const onLoad = async () => {
  // console.log('load')
  const res = await getConsultOrderList(params.value)
  list.value.push(...res.data.rows)
  if(params.value.current < res.data.pageTotal){
    // 准备下一页的页码
    params.value.current++
  }else{
    finished.value = true
  }
  loading.value = false
}
// 删除一条订单
const onDelete = (id: string) => {
  list.value = list.value.filter(item => item.id !== id)
}
</script>

<template>
  <div class="consult-list">
    <van-list 
    v-model:loading="loading" 
    :finished="finished" 
    finished-text="没有更多了" 
    @load="onLoad">
      <consult-item @on-delete="onDelete" :item="item" v-for="item in list" :key="item.id" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.consult-list {
  padding: 10px 15px;
}
</style>